app.controller('BusPriceModalCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax) {

	$scope.dplist = [];
	$scope.dplist.selected = {};

	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	if ($scope.param.type == "update") {
		$scope.copyPrice = angular.copy($scope.param.data);
		$scope.param.change = false;
	} else {
		$scope.param.data.price = 0;
		$scope.param.data.lowest = 0;
		$scope.param.data.parameter = 0;
	}

	//获取站点列表
	$scope.loadStationList = function () {
		var promise = ajax.call(new Req.GET({
			resource: 'route/bs/' + $scope.param.routeId + '/station'
		}));
		promise.then(function (result) {
			$scope.stationList = result.result;
		}, function () { });
	};
	$scope.loadStationList();

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.priceList = [];

	$scope.calc = function () {
		if ($scope.myForm.$valid) {
			if ($scope.dplist.selected == undefined) {
				toaster.pop('warning', '提示', '请选择类型！');
				return;
			}
			$scope.param.data.type = $scope.dplist.selected.id;
			inserter.fields = $scope.param.data;
			inserter.resource = 'price/bs/' + $scope.param.routeId + '/calc';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.priceList = result.result;
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '计算价格失败。');
			});
		}
	};

	$scope.loaddplist = function () {
		$scope.dplist =
			[
				{ id: 0, name: "无折扣" },
				{ id: 1, name: "等额递减" },
				{ id: 2, name: "等差递减" }
			];
		if ($scope.param.data.type != undefined) {
			var id = $scope.param.data.type;
			$scope.dplist.selected = $scope.dplist[id];
		} else {
			$scope.dplist.selected = $scope.dplist[0];
		}
	};
	$scope.loaddplist();

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.dplist.selected == undefined) {
				toaster.pop('warning', '提示', '请选择类型！');
				return;
			}
			if (Number($scope.param.data.price) <= 0 || Number($scope.param.data.lowest) > Number($scope.param.data.price)) {
				toaster.pop('warning', '提示', '基础票价必须大于0且最低票价不得大于基础票价！');
				return;
			}
			$scope.param.data.type = $scope.dplist.selected.id;
			if ($scope.param.data.type == 0) {
				$scope.param.data.lowest = $scope.param.data.price;
				$scope.param.data.parameter = 0;
			}
			if ($scope.param.type == "update") {
				if (angular.toJson($scope.copyPrice) != angular.toJson($scope.param.data)) {
					$scope.param.change = true;
				}
			}
			if ($scope.param.change) {
				ajax.call(new Req.GET({
					resource: 'shift/bs/' + $scope.param.tplId + '/list'
				})).then(function (result) {
					$scope.shiftList = result.result;
					if ($scope.shiftList.length > 0) {
						var modalInstance = $modal.open({
							templateUrl: 'template/bus/shiftTemplate/updateShift.html?v=f9b9b5f55b',
							controller: 'BusShiftTplUpdateCtrl',
							size: 'lg',
							resolve: {
								param: function () {
									return {
										"list": $scope.shiftList
									};
								}
							}
						});
						modalInstance.result.then(function (param) {
							if (param.length > 0) {
								$scope.param.ids = param;
								$modalInstance.close($scope.param);
							} else {
								$modalInstance.close($scope.param);
							}
						});
					} else {
						$modalInstance.close($scope.param);
					}
				}, function () { });
			} else {
				$modalInstance.close($scope.param);
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};

	$scope.noDiscount = false;
	$scope.$watch('dplist.selected', function (newVal, oldVal) {
		if (newVal && newVal.id == 0) {
			$scope.param.data.parameter = 0;
			$scope.param.data.lowest = 0;
			$scope.noDiscount = true;
		} else {
			$scope.noDiscount = false;
		}
	});

});